//
//  RootViewController.m
//  UI06_无限滚动的相册
//
//  Created by dllo on 15/9/21.
//  Copyright (c) 2015年 WangTing. All rights reserved.
//

#import "RootViewController.h"
#define  WIDTH self.view.frame.size.width
#define  HEIGHT self.view.frame.size.height
@interface RootViewController ()<UIScrollViewDelegate>
@property(nonatomic, retain) UIScrollView *scrollView;
@property(nonatomic, retain) UIPageControl *page;
@end

@implementation RootViewController
- (void)dealloc {
    [_scrollView release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor cyanColor];
    
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    [self.view addSubview:self.scrollView];
    [_scrollView release];
    
    // 移动的范围
    self.scrollView.contentSize = CGSizeMake(9 * WIDTH, 0);
    // 按页滚动
    self.scrollView.pagingEnabled = YES;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    
    // 创建UIImageView 放在 UIScrollView上
    UIImageView *firstIImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    firstIImageView.image = [UIImage imageNamed:@"h7.jpeg"];
    [self.scrollView addSubview:firstIImageView];
    [firstIImageView release];
    firstIImageView.userInteractionEnabled = YES;
    
    for (NSInteger i = 1; i < 8; i++) {
      NSString *imageName = [NSString stringWithFormat:@"h%ld.jpeg", i];
      UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH * i, 0, WIDTH, HEIGHT)];
        imageView.image = [UIImage imageNamed:imageName];
        [self.scrollView addSubview:imageView];
        [imageView release];
        imageView.userInteractionEnabled = YES;
    }
   // 为最后面加上一个视图, 放在最后, 显示第一张图片
    UIImageView *lastImageView = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH * 8, 0, WIDTH, HEIGHT)];
    lastImageView.image = [UIImage imageNamed:@"h1.jpeg"];
    [self.scrollView addSubview:lastImageView];
    [lastImageView release];
    lastImageView.userInteractionEnabled = YES;

    // 为了能显示第一张图片, 我们需要先设置一个偏移量, 这样能直接显示罗宾的第一张图片
    self.scrollView.contentOffset = CGPointMake(WIDTH, 0);
    // 设置代理人
    self.scrollView.delegate = self;
    
    // 创建一个pageControl
    self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(100, 600, 200, 40)];
    [self.view addSubview:self.page];
    self.page.backgroundColor = [UIColor clearColor];
    [_page release];
    self.page.numberOfPages = 7;
    // 设置未被点得颜色
    self.page.pageIndicatorTintColor = [UIColor redColor];
    // 选中的点得颜色
    self.page.currentPageIndicatorTintColor = [UIColor yellowColor];
    
    [self.page addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];
    // 设置scrollView实现缩放功能, 需要先设置最大最小的缩放比例
    self.scrollView.minimumZoomScale = 0.5;
    self.scrollView.maximumZoomScale = 2;
    
}
#pragma mark 这个方法用来管理scrollView的缩放功能
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return [scrollView.subviews firstObject];
}


- (void)pageAction: (UIPageControl *)page {
    // 还是设置偏移量
    NSLog(@"%ld", page.currentPage);
    [self.scrollView setContentOffset:CGPointMake((page.currentPage + 1) * WIDTH, 0) animated:YES];
}


// 滑动结束之后会触发的协议方法
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (self.scrollView.contentOffset.x == 8 * WIDTH) {
        self.scrollView.contentOffset = CGPointMake(WIDTH, 0);
    }else if (self.scrollView.contentOffset.x == 0) {
        self.scrollView.contentOffset = CGPointMake(7 * WIDTH, 0);
    }
    // pageControl 和 scrollView 进行关联, 点得移动方向和scrollView的偏移量有关
    
    // 无论怎么方向滑动, 当pageControl 和 scrollView 关联的时候, 偏移量都是修改好的, 所以可以正确的滚动和显示
    self.page.currentPage = self.scrollView.contentOffset.x / WIDTH - 1;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
